<?php

class Kieumau_Controller_MainController extends Zend_Controller_Action
{
	protected	$language;
	
	public function init(){
		
		parent::init();
		
		$this->language = new Kieumau_Db_Language();
		$this->view->language = $this->language;
		
		//Xử lý phần đa ngôn ngữ
		//Kiểm tra xem đã có session default language chưa
		$lang = new Zend_Session_Namespace ( 'defaultLanguage' );
		$lang->unsetAll();
		if(empty($lang->defaultLanguage)){//Chưa có session thì tạo mới
			$defaultLanguage = $this->language->getLanguageDefault();
			$this->defaultLanguage 		= $defaultLanguage['code'];
			$this->defaultLanguageId 	= $defaultLanguage['id'];
			 
			$lang->defaultLanguage 		= $defaultLanguage['code'];//Ghi code vào session
			$lang->defaultLanguageId 	= $defaultLanguage['id'];//Ghi id vào session
		
			require_once('Zend/Session.php');
			$seconds  = 60 * 60 * 24 * 7; // 7 ngày
			Zend_Session::RememberMe($seconds);
		}else{//Có session thì lấy trong session ra
			$this->defaultLanguage = $lang->defaultLanguage;
			$this->defaultLanguageId = $lang->defaultLanguageId;
		}
		
		$this->view->defaultLanguage 	= $this->defaultLanguage;
		$this->view->defaultLanguageId 	= $this->defaultLanguageId;
		
	}	
	/*
		Change the format of date
		m/d/Y	=> Y-m-d
		Y-m-d	=> m/d/Y
	*/
	function formatDate($date, $delimiter = '/'){
		switch($delimiter){
			case '/' : 
				$arrayDate = explode($delimiter, $date);
				if(is_array($arrayDate) && count($arrayDate) == 3)
				$date = $arrayDate[2].'-'.$arrayDate[1].'-'.$arrayDate[0];
				break;
			case '-' : 
				$arrayDate = explode($delimiter, $date);
				if(is_array($arrayDate) && count($arrayDate) == 3)
				$date = $arrayDate[1].'/'.$arrayDate[2].'/'.$arrayDate[0];
			break;   			
		}
		return $date;
	}
	// @author: Peter.Nguyen
	// $date: 5/24/2012
	// @function: create select
	// @return: string
	public function createSelect($data, $value="", $vDisplay="/"){
		try{
			
			$strSelect = '<option value="0">'.$vDisplay.'</option>';
			if(is_array($data)){
				foreach($data as $k=>$v){
					if($k == $value){
						$strSelect .= '<option value="'.$k.'" selected="selected">'.$v.'</option>';
					}else{
						$strSelect .= '<option value="'.$k.'">'.$v.'</option>';	
					}
				}
			}
			return $strSelect;
			
		}catch(Exception  $e){
			echo $e->getMessage();
			exit();
		}
		return;
	}	
	
	// @author: Miken.Tran
	// $date: 06/15/2012
	// @function: create normal select
	// @return: string
	public function createSelectNormal($data, $value=""){
		try{
			$strSelect = "";
			if(is_array($data)){
				foreach($data as $k=>$v){
					if($k == $value){
						$strSelect .= '<option value="'.$k.'" selected="selected">'.$v.'</option>';
					}else{
						$strSelect .= '<option value="'.$k.'">'.$v.'</option>';	
					}
				}
			}
			return $strSelect;
			
		}catch(Exception  $ex){
			echo "<br/> Can not create select, <br/>Error: ".$ex->getMessage();
			exit();
		}
		return;
	}
	
	// @author: Miken.Tran
	// $date: 07/22/2012
	// @function: create normal select, can be use direct in controller with 1 row of code
	// @return: string
	// @input the correct input, and output a string to be put in a select box
	// @$value 	= $oneItem[$params['value']];
	// @$text 	= $oneItem[$params['text']];
	public function createSelectCtrller($params, $selectedValue = ""){
		try{
			
			$commonTable = new Kieumau_Db_Common();	//Register Common Table
			$selectBoxData = array();
			$strSelect = "";
			$listNewsType = $commonTable->getAll($params['tableName'], $params['where'], $params['params']);
			
			if(is_array($listNewsType)){
				foreach($listNewsType as $oneItem){
					if($oneItem[$params['value']] == $selectedValue){
						$strSelect .= '<option value="'.$oneItem[$params['value']].'" selected="selected">'.$oneItem[$params['text']].'</option>';
					}else{
						$strSelect .= '<option value="'.$oneItem[$params['value']].'">'.$oneItem[$params['text']].'</option>';
					}
				}
			}
			return $strSelect;
		}catch(Exception $ex){
			echo "<br/> Can not create select from Controller, <br/>Error: ".$ex->getMessage();
			exit();
		}
	}
	
	
	/*
	 Dùng cho các loại upload file
	Input: 	filename, bao gồm cả extension.	Ví dụ: "pringSummer.jpg"		"day la hinh anh.flv"
	Output:	filename đã loại bỏ những phần thừa.
	/**/
	
	// @author: Miken.Tran
	// $date: 07/24/2012
	// @function: Change name of file upload to correct name
	// @return: string
	function changeName($filename, $prefix = 'pic') {
		//Lấy phần mở rộng của file
		$ext = pathinfo ( $filename, PATHINFO_EXTENSION );
	
		//Thay thế tên file bằng tên file hợp lệ
		$filename = time() . mt_rand(5, 99999999);
	
		//Gắn các thành phần lại thành filename
		$filename = $prefix . '-' . $filename . '.' . $ext;
		
		return $filename;
	}
	
}